import requests
from urllib.parse import urlencode
from common import calc_sign, config
import sys
import time
import random

SERVER_ADDR = "aircon_remote.cuilili.net"


def main():
    if len(sys.argv) != 2:
        print("usage: %s on|off|info" % sys.argv[0])
        sys.exit(1)
    cmd = sys.argv[1]
    api = "/api/admin/aircond/%s" % cmd
    data = {
        "timestamp": int(time.time()),
        "nonce": random.randint(0, 1000),
        "clientId": "1",
    }
    req_data = urlencode(data)  # 将字典类型的请求数据转变为url编码
    api += '?' + req_data
    sign = calc_sign(api, isadmin=True)
    api += "&sign=" + sign
    url = "http://" + SERVER_ADDR + api
    print(url)
    resp = requests.get(url)
    print(resp.text)


if __name__ == '__main__':
    main()
